package com.nondo.dean.product.controller.api.inner;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.nondo.dean.common.annotation.ApiVersion;
import com.nondo.dean.common.web.View;
import com.nondo.dean.product.domain.Product;
import com.nondo.dean.product.domain.ProductMix;
import com.nondo.dean.product.model.param.ProductOrderParam;
import com.nondo.dean.product.model.result.ProductBeanResult;
import com.nondo.dean.product.model.result.ProductResult;
import com.nondo.dean.product.service.ProductMixService;
import com.nondo.dean.product.service.ProductService;
import com.nondo.dean.product.service.PurchaseService;

/**
 * @author ken.
 * @version 1.0
 * @email 695093513@qq.com
 * @date 2018/3/27
 */
@RestController("apiInnerProduct")
@RequestMapping("/{v}/inner/product")
public class ProductController {

    @Autowired
    private ProductService productService;
    
    @Autowired
    private PurchaseService purchaseService;
    
    @Autowired
    private ProductMixService productMixService;

    /**
     * 接收sap返回的产品物料数据入库
     * @param result
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/product")
    public int synSapProduct(@RequestBody String result){
        Object jsonObject = JSON.parse(result);
        String jsonString = jsonObject.toString();
        List<Product> productList = JSONObject.parseArray(jsonString, Product.class);
        int count = productService.SynMaterial(productList);

        return count;
    }

    /**
     * 接收sap返回的库存入库
     * @param resultInventory
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/inventory")
    public String synSapProductInventory(@RequestBody String resultInventory){
        Object jsonObject = JSON.parse(resultInventory);
        String jsonString = jsonObject.toString();
        List<Product> products=JSON.parseArray(jsonString, Product.class);
        productService.synProductInventory(products);

        return "true";
    }
    
    /**
     * 根据产品ID查询单个商品信息
     *
     * @param request
     * @param response
     * @param id 产品ID
     * @return 商品详细信息 {@link Product}
     */
    @ApiVersion(1)
    @RequestMapping("/info/{id}")
    public View<Product> info(HttpServletRequest request, HttpServletResponse response, @PathVariable Integer id) {
        View<Product> view = new View<Product>(20000, "获取数据成功");

        Product product = this.productService.selectById(id);
        
        view.setData(product);

        return view;
    }
    
    /**
     * 根据产品编码查询单个商品信息
     *
     * @param request
     * @param response
     * @param code 产品编码
     * @return 商品详细信息 {@link Product}
     */
    @ApiVersion(1)
    @RequestMapping("/get/{code}")
    public View<Product> get(HttpServletRequest request, HttpServletResponse response, @PathVariable String code) {
        View<Product> view = new View<Product>(20000, "获取数据成功");

        Product product = this.productService.selectByCode(code);
        
        view.setData(product);

        return view;
    }
    
    /**
     * 查询单个商品信息
     *
     * @param request
     * @param response
     * @param id 产品ID
     * @return 商品主体信息 {@link ProductBeanResult}
     */
    @ApiVersion(1)
    @RequestMapping("/result/{id}")
    public View<ProductResult> result(HttpServletRequest request, HttpServletResponse response, @PathVariable Integer id) {
        View<ProductResult> view = new View<ProductResult>(20000, "获取数据成功");

        ProductResult productResult = this.productService.getProductResultById(id);
        
        view.setData(productResult);

        return view;
    }
    
    /**
     * 查询多个商品信息
     *
     * @param request
     * @param response
     * @param ids 多个用逗号分隔的产品ID
     * @return 商品主体信息列表 List<{@link ProductBeanResult}>
     */
    @ApiVersion(1)
    @RequestMapping("/list/{ids}")
    public View<List<ProductResult>> list(HttpServletRequest request, HttpServletResponse response, @PathVariable String ids) {
        View<List<ProductResult>> view = new View<List<ProductResult>>(20000, "获取数据成功");
        List<ProductResult> productResultList = new ArrayList<ProductResult>();
        for (String id : ids.split(",")) {
            if(!"".equals(id)) {
                ProductResult productResult = this.productService.getProductResultById(Integer.parseInt(id));
                productResultList.add(productResult);
            }
        }

        view.setData(productResultList);

        return view;
    }
    
    /**
     * 减库存加销量
     * @param productId 产品ID
     * @param purchaseNum 采购产品数量
     * @return 影响行数
     */
    @ApiVersion(1)
    @RequestMapping("/stock/update")
    public View<Integer> updateStockSaleNum(HttpServletRequest request, HttpServletResponse response, @RequestBody ProductOrderParam orderParam) {
        View<Integer> view = new View<Integer>(20000, "获取数据成功");
        int update = this.productService.updateStockSaleNum(orderParam);
        view.setData(update);
        return view;
    }

    /**
     * 批量修改产品属性
     * @return 影响行数
     */
    @ApiVersion(1)
    @RequestMapping("/properties/update")
    public View<Integer> batchUpdateProductProperties(HttpServletRequest request, HttpServletResponse response) {
        View<Integer> view = new View<Integer>(20000, "获取数据成功");
        int update = this.productService.batchUpdateProductProperties();
        view.setData(update);
        return view;
    }
    
    /**
     * 查找产品所参与的产品组合列表
     * @param productId 产品ID
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/mix/list/{productId}")
    public View<List<ProductMix>> getProductMixList(@PathVariable Integer productId) {
        View<List<ProductMix>> view = new View<List<ProductMix>>(20000, "获取数据成功");
        List<ProductMix> productMixList = this.productMixService.getProductMixList(productId);
        view.setData(productMixList);
        return view;
    }
    
    /**
     * 获取推荐产品列表
     * @return 推荐产品列表的List<{@link Product}>
     */
    @ApiVersion(1)
    @RequestMapping("/recommand/list")
    public View<List<Product>> getRecommandProductList() {
        View<List<Product>> view = new View<List<Product>>(20000, "获取数据成功");
        List<Product> recommendList = this.purchaseService.getRecommandProductList();
        view.setData(recommendList);
        return view;
    }
}
